<?php
require_once "classdb.php";

// class user **********************************************************************************
class user extends db
{
    private $table = "user";
    public $id = NULL;
    public $username = NULL;
    public $password = NULL;
    public $hoten = NULL;
    public $diachi = NULL;
    public $dienthoai = NULL;
    public $email = NULL;
    public $ngay = NULL;
    public $thutu = -1;
    public $anhien = NULL;
    public $idgroup = NULL;

    function dt_set_id($id)
    {
        $this->id = $id;
    }

    function dt_set_username($username)
    {
        $this->username = $username;
    }

    function dt_set_password($password)
    {
        $this->password = $password;
    }

    function dt_set_hoten($hoten)
    {
        $this->hoten = $hoten;
    }

    function dt_set_diachi($diachi)
    {
        $this->diachi = $diachi;
    }

    function dt_set_dienthoai($dienthoai)
    {
        $this->dienthoai = $dienthoai;
    }

    function dt_set_email($email)
    {
        $this->email = $email;
    }

    function dt_set_ngay($ngay)
    {
        $this->ngay = $ngay;
    }

    function dt_set_thutu($thutu)
    {
        $this->thutu = $thutu;
    }

    function dt_set_anhien($anhien)
    {
        $this->anhien = $anhien;
    }

    function dt_set_idgroup($idgroup)
    {
        $this->idgroup = $idgroup;
    }


    function get_by_thutu($id = -1, $anhien = -1)
    {
        return $this->get_by_thutu_db($this->table, $id, $anhien);
    }

    function get_by_ngay($id = -1, $anhien = -1)
    {
        return $this->get_by_ngay_db($this->table, $id, $anhien);
    }

    function get_by_thutu_limit($offset, $records_per_page, $text = "", $anhien = -1)
    {
        return $this->get_by_thutu_limit_db($this->table, $offset, $records_per_page, $text, $anhien);
    }

    function get_by_ngay_limit($offset, $records_per_page, $text = "", $anhien = -1)
    {
        return $this->get_by_ngay_limit_db($this->table, $offset, $records_per_page, $text, $anhien);
    }

    function get_max_count($text = "", $anhien = -1)
    {
        return $this->get_max_count_db($this->table, $text, $anhien);
    }

    function get_by_parent($table_parent, $field, $idparent)
    {
        return $this->get_by_parent_db($this->table, $table_parent, $field, $idparent);
    }

    function change_anhien($id)
    {
        $this->change_anhien_db($this->table, $id);
    }

    function change_anhien_list($listid)
    {
        $this->change_anhien_list_db($this->table, $listid);
    }

    function delete($id)
    {
        $this->delete_data_db($this->table, $id);
    }

    function delete_list($listid)
    {
        $this->delete_list_data_db($this->table, $listid);
    }

    function update_field($id, $field, $value, $anhien = -1)
    {
        $this->update_field_db($this->table, $id, $field, $value, $anhien);
    }

    /* 
    hàm update này chỉ dùng để update 1 đối tượng 
    được set dầy dủ các biến luôn cả "id" nhá
    Lưu ý: nếu ko set giá trị dầy đủ thi field nào ko set nó se~ ra null
    cho nen nếu mún update 1 filed thì nen xài hàm update_field() phía trên 
    dể update 1 field nhá  !!!!!!!
    */
    function update()
    {
        if ($this->id != NULL && $this->id > 0) {
            $list_field = $this->show_field_db($this->table);
            $query      = "UPDATE " . $this->table . " SET ";
            $flag       = 0;
            foreach ($list_field as $field) {
                if ($flag == 1)
                    $query = $query . $field[0] . "='" . $this->$field[0] . "',";
                $flag = 1;
            }

            $query = substr($query, 0, strlen($query) - 1);
            $query = $query . " WHERE id='$this->id'";
            mysql_query($query) or die(mysql_error());
        }
    }

    function create()
    {
        // mún kiểm tra gì thì viết ở đây

        // code cứng không cần sửa **********************
        if ($this->thutu == -1)
            $this->thutu = $this->get_max_thutu_db($this->table) + 1;
        $list_field = $this->show_field_db($this->table);
        $query      = "INSERT INTO " . $this->table . " (";
        $flag       = 0;
        foreach ($list_field as $field) {
            if ($flag == 1)
                $query = $query . $field[0] . ",";
            $flag = 1;
        }
        $query = substr($query, 0, strlen($query) - 1);
        $query = $query . ") VALUES (";
        $flag  = 0;
        foreach ($list_field as $field) {
            if ($flag == 1)
                $query = $query . "'" . $this->$field[0] . "',";
            $flag = 1;
        }
        $query = substr($query, 0, strlen($query) - 1);
        $query = $query . ")";
        mysql_query($query) or die(mysql_error());
        // ***********************************************
    }

    function get_by_username($username)
    {
        $query = "SELECT * FROM " . $this->table . " WHERE username='$username'";
        return $this->get_data_db($query);
    }

    function get_by_ngay_admin()
    {
        $query = "SELECT * FROM " . $this->table . " WHERE idgroup<>-1 order by ngay DESC";
        return $this->get_list_data_db($query);
    }
}

// class tin_theloai **********************************************************************************
class quantri extends db
{
    function rootlist($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tin_root", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function tin_theloailist($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tin_theloai", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function tin_loailist($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tin_loai", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function tin_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tin", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function lienhe_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "lienhe", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function tintuc_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tintuc", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function field_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "field", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function quangcao_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "quangcao", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function lienketweb_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "lienketweb", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function tuyendung_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tuyendung", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function dichvu_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "dichvu", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function tintuc1_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tintuc1", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function thongtin_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "thongtin", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function thongtinkhac_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "thongtinkhac", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function dauso_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "dauso", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function nhadep_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "nhadep", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function dongia_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "dongia", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function thicong_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "thicong", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function sanpham_chungloai_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "sanpham_chungloai", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function sanpham_loai_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "sanpham_loai", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function sanpham_loailop_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "sanpham_loailop", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function seo_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "seo", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function contact_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "contact", -1, -1, "and hoten like '%$tukhoa%'", '');
    }

    function sanpham_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "sanpham", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function sanpham_list2($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '', $option = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "sanpham", -1, -1, "and ten_vi like '%$tukhoa%'" . $option, '');
    }

    function tin_loai_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tin_loai", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function donhang_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_order($pageNum, $pageSize, $totalRows, "donhang", -1, -1, "and tennguoinhan like '%$tukhoa%'", ' order by datratien desc');
    }

    function thietke_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "thietke", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function nhac_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "nhac", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function album_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "album", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function album_photo_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "album_photo", -1, -1, "and ten_vi like '%$tukhoa%'", '');
    }

    function users_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_order($pageNum, $pageSize, $totalRows, "users", -1, -1, "and (email like '%$tukhoa%' or hoten like '%$tukhoa%')", ' order by id desc');
    }

    function yeucausim_list($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '')
    {
        return $this->get_phantrang_order($pageNum, $pageSize, $totalRows, "yeucausim", -1, -1, "and (email like '%$tukhoa%' or hoten like '%$tukhoa%')", ' order by id desc');
    }

    /**
     * Tạo danh sách option của tất cả menu trong hệ thống
     */
    function createMenuSelectBox($idc, $tablec) {
        $table = 'sanpham_menu';
        $list_menu = $this->get_by_order($table, -1, 1);
        while ($row = mysql_fetch_assoc($list_menu)) {
            $id_menu = $row['id'];
            echo '<option '.$this->compare($id_menu, $idc, $table, $tablec).' data-value="sanpham_menu" value="' . $row['id'] . '">' . $row['ten_vi'] . '</option>';
            $table = 'sanpham_root';
            $list_root = $this->get_by_order($table, -1, 1, ' and idmenu =' . $id_menu);
            while ($row1 = mysql_fetch_assoc($list_root)) {
                $id_root        = $row1['id'];
                echo '<option '.$this->compare($id_root, $idc, $table, $tablec).' data-value="sanpham_root" value="' . $row1['id'] . '">--' . $row1['ten_vi'] . '</option>';
                $table = 'sanpham_chungloai';
                $list_chungloai = $this->get_by_order($table, -1, 1, ' and idroot=' . $id_root);
                while ($row2 = mysql_fetch_assoc($list_chungloai)) {
                    $id_chungloai = $row2['id'];
                    echo '<option '.$this->compare($id_chungloai, $idc, $table, $tablec).' data-value="sanpham_chungloai" value="' . $row2['id'] . '">----' . $row2['ten_vi'] . '</option>';
                    $table = 'sanpham_loai';
                    $list_loai    = $this->get_by_order($table, -1, 1, ' and idcl=' . $id_chungloai);
                    while ($row3 = mysql_fetch_assoc($list_loai)) {
                        echo '<option '.$this->compare($row3['id'], $idc, $table, $tablec).' data-value="sanpham_loai" value="' . $row3['id'] . '">------' . $row3['ten_vi'] . '</option>';
                    }
                }
            }
        }
    }

    /**
     * @param $id là id hiện tại của vòng lặp
     * @param $idc là id để so sánh
     * @param $table là table hiện tại của vòng lặp
     * @param $tablec Đây là table truyền vào để so sánh
     * @return trả về chuổi selected="selected" nếu trùng
     */
    protected  function compare($id, $idc, $table, $tablec) {
        if($id == $idc && $table == $tablec) {
            return 'selected="selected"';
        }
        return '';
    }

}

?>